package ide.ccf.ccdesigner.views.embedded;

import ide.ccf.ccproject.common.JambiCompatible;

import java.io.IOException;
import java.util.*;

import org.eclipse.swt.*;
import org.eclipse.swt.graphics.*;
import org.eclipse.swt.widgets.*;
import org.eclipse.swt.events.*;
import org.eclipse.core.runtime.FileLocator;
import org.eclipse.core.runtime.Platform;
import org.osgi.framework.Bundle;

/**
 * 属性编辑器视图
 *
 */
public class PropertyEditorW extends Composite implements JambiCompatible
{
    long handleWidget;
    @SuppressWarnings("rawtypes")
	static Hashtable table = new Hashtable();
    
    /**
     * 构造器
     * @param parent
     * @param style
     */
    @SuppressWarnings({ "rawtypes", "unchecked" })
	public PropertyEditorW(Composite parent, int style)
    {
        super(parent, style);
        
        try 
        {
            org.osgi.framework.Bundle bundle = org.eclipse.core.runtime.Platform.getBundle("ide.ccf.ccdesigner");
            
            if (bundle != null) 
            {
                Class cls = bundle.loadClass("ide.ccf.ccdesigner.cppjambi.QtJambiConnection");
                java.lang.reflect.Method m = cls.getMethod("validateJambi", new Class[] { JambiCompatible.class });
                m.invoke(null, new Object[] { this });
            }
        } 
        catch (Throwable t) 
        {
            Bundle pluginBundle = Platform.getBundle("ide.ccf.ccdesigner." + Platform.getOS() + "." + Platform.getOSArch());
            
            if (pluginBundle != null) 
            {
                try 
                {
                    String path = FileLocator.toFileURL(FileLocator.find(pluginBundle, new org.eclipse.core.runtime.Path("lib/libccfdesigner.so"), null)).getPath();
                    System.load(path);
                } 
                catch (IOException ex) 
                {
                    ex.printStackTrace();
                } 
                catch (UnsatisfiedLinkError ex) 
                {
                    ex.printStackTrace();
                }
            }
        }

        handleWidget = createControl(handle, embeddedHandle);
        
        if (handleWidget == 0) 
        	SWT.error (SWT.ERROR_NO_HANDLES);
        
        table.put(new Long(handleWidget), this);
        
        setFont(getFont());
    
        addDisposeListener(new DisposeListener() 
        {
            public void widgetDisposed(DisposeEvent e) 
            {
                PropertyEditorW.this.widgetDisposed(e);
            }
        });

        addControlListener(new ControlAdapter()
        {
            public void controlResized(ControlEvent e) 
            {
                PropertyEditorW.this.controlResized(e);
            }
        });

        try 
        {
            org.osgi.framework.Bundle bundle = org.eclipse.core.runtime.Platform.getBundle("ide.ccf.ccdesigner");
            
            if (bundle != null) 
            {
                Class cls = bundle.loadClass("ide.ccf.ccdesigner.cppjambi.QtJambiConnection");
                java.lang.reflect.Method m = cls.getMethod("loadPlugins", new Class[] { JambiCompatible.class });
                m.invoke(null, new Object[] { this });
            }
        } 
        catch (Throwable t) 
        { 
        	
        }

        initialize();
    }

    /**
     * 装载库
     * 需要在一个类中调用，这个类将会被调用本地方法的同一个类装载器装载
     */
    public void loadLibrary(String path) 
    {
        System.load(path);
    }
    
    /**
     * 释放
     * @param e
     */
    public void widgetDisposed(DisposeEvent e)
    {
        table.remove(new Long(handleWidget));
        disposeControl(handleWidget);
        handleWidget = 0;
    }
  
    /**
     * 重绘大小
     * @param e
     */
    public void controlResized(ControlEvent e)
    {
        Rectangle rect = getClientArea();
        resizeControl(handleWidget, rect.x, rect.y, rect.width, rect.height);
    }

    /**
     * 计算大小
     */
    public Point computeSize(int wHint, int hHint, boolean changed)
    {
        checkWidget();
        int [] result = new int[2];
        
        computeSize(handleWidget, result);
        
        if (wHint != SWT.DEFAULT) 
        	result[0] = wHint;
        
        if (hHint != SWT.DEFAULT) 
        	result[1] = hHint;
        
        int border = getBorderWidth();
        
        return new Point(result[0] + border * 2, result[1] + border * 2);
    }
    
    /**
     * 设置字体
     */
    public void setFont(Font font)
    {
        super.setFont(font);
        
        if (font == null)
            return;
        
        FontData[] fntlist = font.getFontData();
        setFont(handleWidget, fntlist[0].getName(), fntlist[0].getHeight());
    }
    
    /**
     * 插件失败信息
     */
    public String pluginFailureString()
    {
        checkWidget();
        return pluginFailureString(handleWidget);
    }

    /**
     * 清空表格数据
     */
    public void clearSheet()
    {
        checkWidget();
        clearSheet(handleWidget);
    }

    /**
     * 初始化Jambi插件
     */
    public boolean initializeJambiPlugins(String jambiBase, String jambiPluginPath, String customWidgetClassPath, String resourcePath, String jvm)
    {
        checkWidget();
        return initializeJambiPlugins(handleWidget, jambiBase, jambiPluginPath, customWidgetClassPath, resourcePath, jvm);
    }

    /**
     * 初始化
     */
    public void initialize()
    {
        checkWidget();
        initialize(handleWidget);
    }

    /**
     * 更新自定义控件路径
     */
    public void updateCustomWidgetLocation(String path)
    {
        checkWidget();
        updateCustomWidgetLocation(handleWidget, path);
    }

    static final native long createControl(long phandle, long socketWin);
    static final native void computeSize(long handle, int [] result);
    static final native void disposeControl(long handle);
    static final native void resizeControl(long handle, int x, int y, int width, int height);
    static final native void setFont(long handle, String family, int size);
    static final native String pluginFailureString(long handle);
    static final native void clearSheet(long handle);
    static final native boolean initializeJambiPlugins(long handle, String jambiBase, String jambiPluginPath, String customWidgetClassPath, String resourcePath, String jvm);
    static final native void initialize(long handle);
    static final native void updateCustomWidgetLocation(long handle, String path);
}
